﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class MicroWebsiteGoods
    {
        public int Add(Chain.Model.MicroWebsiteGoods model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MicroWebsiteGoods(");
            builder.Append("MicroGoodsCode,MicroGoodsClassID,MicroGoodsName,MicroSalePrice,MicroPrice,MicroPoint,MicroGoodsBidPrice,MicroGoodsRemark,MicroGoodsPicture,MicroGoodsCreateTime,MicroGoodsShopID)");
            builder.Append(" values (");
            builder.Append("@MicroGoodsCode,@MicroGoodsClassID,@MicroGoodsName,@MicroSalePrice,@MicroPrice,@MicroPoint,@MicroGoodsBidPrice,@MicroGoodsRemark,@MicroGoodsPicture,@MicroGoodsCreateTime,@MicroGoodsShopID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroGoodsCode", SqlDbType.NVarChar, 50), new SqlParameter("@MicroGoodsClassID", SqlDbType.Int, 4), new SqlParameter("@MicroGoodsName", SqlDbType.NVarChar, 200), new SqlParameter("@MicroSalePrice", SqlDbType.Money, 8), new SqlParameter("@MicroPrice", SqlDbType.Money, 8), new SqlParameter("@MicroPoint", SqlDbType.Int, 4), new SqlParameter("@MicroGoodsBidPrice", SqlDbType.Money, 8), new SqlParameter("@MicroGoodsRemark", SqlDbType.NVarChar, 0x7d0), new SqlParameter("@MicroGoodsPicture", SqlDbType.NVarChar, 200), new SqlParameter("@MicroGoodsCreateTime", SqlDbType.DateTime), new SqlParameter("@MicroGoodsShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MicroGoodsCode;
            cmdParms[1].Value = model.MicroGoodsClassID;
            cmdParms[2].Value = model.MicroGoodsName;
            cmdParms[3].Value = model.MicroSalePrice;
            cmdParms[4].Value = model.MicroPrice;
            cmdParms[5].Value = model.MicroPoint;
            cmdParms[6].Value = model.MicroGoodsBidPrice;
            cmdParms[7].Value = model.MicroGoodsRemark;
            cmdParms[8].Value = model.MicroGoodsPicture;
            cmdParms[9].Value = model.MicroGoodsCreateTime;
            cmdParms[10].Value = model.MicroGoodsShopID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int MicroGoodsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MicroWebsiteGoods ");
            builder.Append(" where MicroGoodsID=@MicroGoodsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroGoodsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MicroGoodsID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string MicroGoodsIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MicroWebsiteGoods ");
            builder.Append(" where MicroGoodsID in (" + MicroGoodsIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int MicroGoodsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MicroWebsiteGoods");
            builder.Append(" where MicroGoodsID=@MicroGoodsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroGoodsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MicroGoodsID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(string strGoodsCode, int ShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MicroWebsiteGoods");
            builder.Append(" where MicroGoodsCode=@MicroGoodsCode and MicroGoodsShopID=@MicroGoodsShopID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroGoodsCode", SqlDbType.NVarChar, 50), new SqlParameter("@MicroGoodsShopID", SqlDbType.Int) };
            cmdParms[0].Value = strGoodsCode;
            cmdParms[1].Value = ShopID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(int goodsID, string goodsCode, int ShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select count(1) from MicroWebsiteGoods");
            builder.Append(" where MicroGoodsCode=@MicroGoodsCode and MicroGoodsID<>@MicroGoodsID and MicroGoodsShopID<>@MicroGoodsShopID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroGoodsCode", SqlDbType.NVarChar, 50), new SqlParameter("@MicroGoodsID", SqlDbType.Int), new SqlParameter("@MicroGoodsShopID", SqlDbType.Int) };
            cmdParms[0].Value = goodsCode;
            cmdParms[1].Value = goodsID;
            cmdParms[2].Value = ShopID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MicroGoodsID,MicroGoodsCode,MicroGoodsClassID,MicroGoodsName,MicroSalePrice,MicroPrice,MicroPoint,MicroGoodsBidPrice,MicroGoodsRemark,MicroGoodsPicture,MicroGoodsCreateTime,MicroGoodsShopID ");
            builder.Append(" FROM MicroWebsiteGoods ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" MicroGoodsID,MicroGoodsCode,MicroGoodsClassID,MicroGoodsName,MicroSalePrice,MicroPrice,MicroPoint,MicroGoodsBidPrice,MicroGoodsRemark,MicroGoodsPicture,MicroGoodsCreateTime,MicroGoodsShopID ");
            builder.Append(" FROM MicroWebsiteGoods ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.MicroGoodsID desc");
            }
            builder.Append(")AS Row, T.*  from MicroWebsiteGoods T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "MicroWebsiteGoods,MicroWebsiteGoodsClass";
            string[] column = new string[] { "MicroWebsiteGoods.*,MicroGoodsClassName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "MicroGoodsCreateTime", "MicroGoodsID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("MicroGoodsID", "MicroWebsiteGoods");
        }

        public Chain.Model.MicroWebsiteGoods GetModel(int MicroGoodsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 MicroGoodsID,MicroGoodsCode,MicroGoodsClassID,MicroGoodsName,MicroSalePrice,MicroPrice,MicroPoint,MicroGoodsBidPrice,MicroGoodsRemark,MicroGoodsPicture,MicroGoodsCreateTime,MicroGoodsShopID from MicroWebsiteGoods ");
            builder.Append(" where MicroGoodsID=@MicroGoodsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroGoodsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MicroGoodsID;
            Chain.Model.MicroWebsiteGoods goods = new Chain.Model.MicroWebsiteGoods();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["MicroGoodsID"] != null) && (set.Tables[0].Rows[0]["MicroGoodsID"].ToString() != ""))
            {
                goods.MicroGoodsID = int.Parse(set.Tables[0].Rows[0]["MicroGoodsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroGoodsCode"] != null) && (set.Tables[0].Rows[0]["MicroGoodsCode"].ToString() != ""))
            {
                goods.MicroGoodsCode = set.Tables[0].Rows[0]["MicroGoodsCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MicroGoodsClassID"] != null) && (set.Tables[0].Rows[0]["MicroGoodsClassID"].ToString() != ""))
            {
                goods.MicroGoodsClassID = int.Parse(set.Tables[0].Rows[0]["MicroGoodsClassID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroGoodsName"] != null) && (set.Tables[0].Rows[0]["MicroGoodsName"].ToString() != ""))
            {
                goods.MicroGoodsName = set.Tables[0].Rows[0]["MicroGoodsName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MicroSalePrice"] != null) && (set.Tables[0].Rows[0]["MicroSalePrice"].ToString() != ""))
            {
                goods.MicroSalePrice = decimal.Parse(set.Tables[0].Rows[0]["MicroSalePrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroPrice"] != null) && (set.Tables[0].Rows[0]["MicroPrice"].ToString() != ""))
            {
                goods.MicroPrice = decimal.Parse(set.Tables[0].Rows[0]["MicroPrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroPoint"] != null) && (set.Tables[0].Rows[0]["MicroPoint"].ToString() != ""))
            {
                goods.MicroPoint = int.Parse(set.Tables[0].Rows[0]["MicroPoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroGoodsBidPrice"] != null) && (set.Tables[0].Rows[0]["MicroGoodsBidPrice"].ToString() != ""))
            {
                goods.MicroGoodsBidPrice = decimal.Parse(set.Tables[0].Rows[0]["MicroGoodsBidPrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroGoodsRemark"] != null) && (set.Tables[0].Rows[0]["MicroGoodsRemark"].ToString() != ""))
            {
                goods.MicroGoodsRemark = set.Tables[0].Rows[0]["MicroGoodsRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MicroGoodsPicture"] != null) && (set.Tables[0].Rows[0]["MicroGoodsPicture"].ToString() != ""))
            {
                goods.MicroGoodsPicture = set.Tables[0].Rows[0]["MicroGoodsPicture"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MicroGoodsCreateTime"] != null) && (set.Tables[0].Rows[0]["MicroGoodsCreateTime"].ToString() != ""))
            {
                goods.MicroGoodsCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["MicroGoodsCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroGoodsShopID"] != null) && (set.Tables[0].Rows[0]["MicroGoodsShopID"].ToString() != ""))
            {
                goods.MicroGoodsShopID = int.Parse(set.Tables[0].Rows[0]["MicroGoodsShopID"].ToString());
            }
            return goods;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MicroWebsiteGoods ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.MicroWebsiteGoods model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MicroWebsiteGoods set ");
            builder.Append("MicroGoodsCode=@MicroGoodsCode,");
            builder.Append("MicroGoodsClassID=@MicroGoodsClassID,");
            builder.Append("MicroGoodsName=@MicroGoodsName,");
            builder.Append("MicroSalePrice=@MicroSalePrice,");
            builder.Append("MicroPrice=@MicroPrice,");
            builder.Append("MicroPoint=@MicroPoint,");
            builder.Append("MicroGoodsBidPrice=@MicroGoodsBidPrice,");
            builder.Append("MicroGoodsRemark=@MicroGoodsRemark,");
            builder.Append("MicroGoodsPicture=@MicroGoodsPicture,");
            builder.Append("MicroGoodsCreateTime=@MicroGoodsCreateTime,");
            builder.Append("MicroGoodsShopID=@MicroGoodsShopID");
            builder.Append(" where MicroGoodsID=@MicroGoodsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroGoodsCode", SqlDbType.NVarChar, 50), new SqlParameter("@MicroGoodsClassID", SqlDbType.Int, 4), new SqlParameter("@MicroGoodsName", SqlDbType.NVarChar, 200), new SqlParameter("@MicroSalePrice", SqlDbType.Money, 8), new SqlParameter("@MicroPrice", SqlDbType.Money, 8), new SqlParameter("@MicroPoint", SqlDbType.Int, 4), new SqlParameter("@MicroGoodsBidPrice", SqlDbType.Money, 8), new SqlParameter("@MicroGoodsRemark", SqlDbType.NVarChar, 0x7d0), new SqlParameter("@MicroGoodsPicture", SqlDbType.NVarChar, 200), new SqlParameter("@MicroGoodsCreateTime", SqlDbType.DateTime), new SqlParameter("@MicroGoodsShopID", SqlDbType.Int, 4), new SqlParameter("@MicroGoodsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MicroGoodsCode;
            cmdParms[1].Value = model.MicroGoodsClassID;
            cmdParms[2].Value = model.MicroGoodsName;
            cmdParms[3].Value = model.MicroSalePrice;
            cmdParms[4].Value = model.MicroPrice;
            cmdParms[5].Value = model.MicroPoint;
            cmdParms[6].Value = model.MicroGoodsBidPrice;
            cmdParms[7].Value = model.MicroGoodsRemark;
            cmdParms[8].Value = model.MicroGoodsPicture;
            cmdParms[9].Value = model.MicroGoodsCreateTime;
            cmdParms[10].Value = model.MicroGoodsShopID;
            cmdParms[11].Value = model.MicroGoodsID;
            int num = DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
            if (num > 0)
            {
                return num;
            }
            return 0;
        }
    }
}

